Page({

  /**
   * 页面的初始数据
   */
  data: {
    fruit: [{
      status: 1, // 确认领取
      name: '是',
    }, {
      status: 2, // 未领取
      name: '否'
    }],
    current: '1',
    showPopup: false, // 是否显示弹窗
    certificate: {}, // 证书状态等配置信息
    unReceive: '', // 未领取原因
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.diplomaStatusInfo()
  },
  /**
   * @see 实现双向绑定
   */
  inputChange (e) {
    // console.log(e.detail.detail.value)
    this.setData({
      unReceive: e.detail.detail.value
    })
  },

  /**
   * @see 关闭弹窗
   */
  confirm () {
    this.setData({
      showPopup: false
    })
  },
  /**
   * @see 获取毕业证书领取状态
   */
  diplomaStatusInfo () {
    const userInfo = wx.getStorageSync('userInfo')
    // 请求参数, 学号
    const request = {
      xh: userInfo.userId
    }
    wx.$API['studentInformation/diplomaStatusInfo'](request).then(res => {
      // 拿到毕业证书领取配置信息
      const [certificate] = res.data
      this.setData({
        certificate,
        current: certificate.diploma_affirm_status,
        unReceive: certificate.remark
      })
    })
      .catch(error => {
        wx.showToast({
          title: error,
          icon: 'none'
        })
        console.log('studentInformation/diplomaStatusInfo 获取证书领取状态出错!', error);
      })
  },
  /**
   * @see 修改毕业证书领取状态
   */
  submit () {
    //      配置信息      当前选中的值
    const { certificate, current, unReceive } = this.data
    const request = {
      id: certificate.id,
      diplomaAffirmStatus: current,
      remark: current == 1 ? '' : unReceive
    }
    if (Number(current) === 2 && !unReceive.trim()) {
      wx.showToast({
        title: '请填写原因',
        icon: 'none',
      })
      return
    }
    wx.$API['studentInformation/affirmGetStatus'](request).then(res => {
      // 显示操作成功弹窗
      this.setData({
        showPopup: true
      })
      // 重新获取状态
      this.diplomaStatusInfo()
    })
      .catch(error => {
        wx.showToast({
          title: error,
          icon: 'none'
        })
        console.log('studentInformation 修改毕业证书领取状态出错!', error)
      })
  },
  /** 
   * @see 单选选中事件
  */
  handleFruitChange ({ detail = {} }) {
    console.log(detail);
    if (Number(detail.value) === 2) {
      console.log('不同意,弹窗')
    }
    this.setData({
      current: detail.value
    });
  },
})